# _*_ coding:utf-8 _*_
"""
:File: models.py.py
:Author: cfp
:Date: 2025/9/10 09:41
:LastEditTime: 2025/9/10 09:41
:LastEditors: cfp
:Description: 
"""
from datetime import datetime
from application.common.database import db


class AmazonListing:
    @staticmethod
    async def get_all():
        """获取所有listing"""
        query = "SELECT * FROM asinking_amazonlisting where status=1 and is_delete=false"
        return await db.execute_query(query)

    @staticmethod
    async def get_by_asin(asin):
        """根据ASIN获取listing"""
        query = "SELECT * FROM asinking_amazonlisting WHERE asin = %s"
        return await db.execute_query(query, (asin,))


class AmazonProductData:
    @staticmethod
    async def create(asin, title=None, price=None, status=1, is_delete=0, crawl_time=None):
        """创建产品数据"""
        if crawl_time is None:
            crawl_time = datetime.utcnow()

        query = """
        INSERT INTO amazon_product_data (asin, title, price, status, is_delete, crawl_time)
        VALUES (%s, %s, %s, %s, %s, %s)
        RETURNING id
        """
        result = await db.execute_query(
            query, (asin, title, price, status, is_delete, crawl_time)
        )
        return result[0]['id'] if result else None

    @staticmethod
    async def bulk_create(data_list):
        """批量创建产品数据"""
        query = """
        INSERT INTO amazon_product_data (asin, title, price, status, is_delete, crawl_time)
        VALUES (%s, %s, %s, %s, %s, %s)
        """
        params_list = [
            (item['asin'], item.get('title'), item.get('price'),
             item.get('status', 1), item.get('is_delete', 0),
             item.get('crawl_time', datetime.utcnow()))
            for item in data_list
        ]
        return await db.execute_many(query, params_list)